<?php
require __DIR__ . '/AutoLoad.php';
date_default_timezone_set('PRC');
mb_internal_encoding('UTF-8');

if (!defined('APP_DIR')) {
    echo '请设置常量"APP_DIR"为你的项目目录！' . PHP_EOL;exit;
}

if (!defined('COMMON_DIR')) {
    define('COMMON_DIR', APP_DIR);
}

if (!defined('CONFIG_DIR')) {
	define('CONFIG_DIR', APP_DIR . '/Config');
}


spl_autoload_register(function($className) {
    AutoLoad::includeClass($className);
});

class Main extends Home{

    public static $includeDir;
    public static $appNamespace = '\\';
    public $appName;
    private $route;

    public function __construct() {
        self::init();
    }

    public static function init() {

    }

    public function setRoute($route) {
        $this->route = $route;
    }

	public function api() {
		try {
			$controller = Lib\Core\Url::instance()->getApiController();
			$controller->run();
		} catch (\Lib\Exception\SysExit $e) {
			// system exit.
		}
	}

    public function run() {
        try {
            $controller = Lib\Core\Url::instance()->getController(array('Index', 'Index'), $this->route);
            $controller->run();
        } catch (\Lib\Exception\SysExit $e) {
            // system exit.
        }
    }

    /**
     * @return \Lib\Core\Script
     */
    public function script() {
        $script = \Lib\Core\Script::instance();
        return $script;
    }

    public function server() {
        $server = \Lib\Core\Server::instance();
        $server->start();
    }

    public static function sysExit() {
        throw new \Lib\Exception\SysExit('system exit');
    }

}

class Home {

    private static $class = array();

    /**
     * @return static
     */
    public static function instance() {
        $className = get_called_class();
        if (empty(self::$class[$className])) {
            self::$class[$className] = new $className();
        }
        return self::$class[$className];
    }
}
if (!function_exists('P')) {
	function P() {
		$args = func_get_args(); // 获取多个参数
		if (count($args) < 1) {
			return;
		}

		echo '<div style = "width:100%;text-align:left"><pre>';
		// 多个参数循环输出
		foreach ($args as $arg) {
			if (is_array($arg)) {
				print_r($arg);
				echo '<br>';
			} else if (is_string($arg)) {
				echo $arg . '<br>';
			} else if (is_object($arg)) {
				print_r($arg);
				echo '<br>';
			} else {
				var_dump($arg);
				echo '<br>';
			}
		}
		echo '</pre></div>';
	}
}
function import($file) {
    $dir = COMMON_DIR . '/Import/';
    if (!strpos($file, '.')) {
        $file .= '.php';
    }
    $filePath = $dir . $file;
    require_once $filePath;
}